package com.shanling.shanlingcontroller.http;

import java.util.HashMap;
import java.util.Map;

import okhttp3.OkHttpClient;

/**
 * Created by weiyu on 2019/1/16 0016.
 */

public class BuildFactory {
    private static BuildFactory mInstance;
    private Map<String, Object> serviceMap;
    private RetrofitBuildConfig buildConfig;

    private BuildFactory() {
        serviceMap = new HashMap<>();
        buildConfig = new RetrofitBuildConfig();
    }
    public static BuildFactory getInstance() {
        if (mInstance == null) {
            mInstance = new BuildFactory();
        }
        return mInstance;
    }
    @SuppressWarnings("unchecked")
    public <T> T create(Class<T> serviceClass, String baseUrl) {
        Object serviceObject = serviceMap.get(serviceClass.getName());
        if (serviceObject != null) {
            return (T) serviceObject;
        }
        serviceObject = buildConfig.getBuilder(baseUrl).build().create(serviceClass);
        serviceMap.put(serviceClass.getName(), serviceObject);
        return (T) serviceObject;
    }
    public OkHttpClient getOkClient() {
        return buildConfig.getOkClient();
    }
}
